package com.kiwi.cases.match.matchenter;

import com.google.common.reflect.TypeToken;
import com.kiwi.constant.ConstantURL;
import com.kiwi.util.APIEncryptSet;

import com.netease.shockwave.http.HttpHandler;
import com.netease.shockwave.testng.ShockwaveTestNG;

import com.sdicons.json.helper.JSONMap;
import jxl.read.biff.BiffException;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.databene.benerator.anno.Source;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.*;

import java.io.IOException;
import java.util.*;

import com.kiwi.util.MD5Util;

import static com.kiwi.constant.Constant.COMMON_GSON;
import static com.kiwi.constant.ConstantURL.GAEA_USER_MATCH_DIR;
import static com.kiwi.constant.ConstantURL.PANDORA_MATCH_ENTER_DIR;
import static com.kiwi.util.excel.JxlReader.jxlReader;

/**
 * 测试接口：密码登录接口 http://uc.s.kiwi.com/api/login
 *
 * @param
 * @param
 * @author hancongcong
 */
public class MatchEnter extends ShockwaveTestNG
{
    private static ArrayList<String> sheetNames;
    private  Logger logger = Logger.getLogger(MatchEnter.class);

    @BeforeClass
    public void BeforeClassTest()
    {
        logger.info("@test：用户游戏匹配测试开始");

    }
   /* @Test(description = "用户游戏匹配/match/enter", dataProvider = "feeder")
    @Source("src/com/kiwi/cases/match/matchenter/test.xls")*/

    @Test(description = "用户游戏匹配/match/enter", dataProvider = "MatchEnterResultData")
    public void testMatchEnter(HashMap<String,String> paramAndExpect) throws Exception {
        String param = paramAndExpect.get("param");
        logger.info("@MatchEnter未加密参数："+param);
        //System.out.println("@MatchEnterResult---未加密参数为："+param);
        param = APIEncryptSet.enCode(param);
        //System.out.println("@MatchEnterResult---加密的参数为:"+param);
        String[] paramsEncode = param.split("&");
        String data = paramsEncode[0].substring(5,paramsEncode[0].length());
        String stamp = paramsEncode[1].substring(6,paramsEncode[1].length());
        //System.out.println("@MatchEnterResult---data="+data);
        //System.out.println("@MatchEnterResult---stamp="+stamp);
        HashMap<String, String> httpParams = new HashMap<String, String>();
        httpParams.put("data",data);
        httpParams.put("stamp",stamp);
        HttpHandler handler = HttpHandler.getInstance();
        String enResult = handler.usingPostMethod(ConstantURL.PANDORA_MATCH_ENTER,httpParams,null);
        String expect = paramAndExpect.get("expect");
        String[] expects = expect.split(",");
        String you = expects[0];
        String me = expects[1];
        logger.info("@MatchEnter---请求结果"+me+enResult);
        Map<String,Object> map = COMMON_GSON.fromJson(enResult, new TypeToken<HashMap<String,Object>>(){}.getType());


        //logger.info("@MatchEnterResult---result:"+map.get("result"));
        //logger.info("@MatchEnterResult---resultDesc:"+map.get("resultDesc"));
        //logger.info("@MatchEnterResult---Data:"+map.get("data"));

        String result = String.valueOf((int)Double.parseDouble(map.get("result").toString()));
        //校验code
        //Assert.assertEquals(result,paramAndExpect.get("code"));
    }
    @Test(description = "用户随机匹配结果接口/match/enter/result", dataProvider = "MatchEnterResultData")
    public void testMatchEnterResult(HashMap<String,String> paramAndExpect)throws Exception{
        String param = paramAndExpect.get("param");
        //System.out.println("@MatchEnterResult---未加密参数为："+param);
        param = APIEncryptSet.enCode(param);
        //System.out.println("@MatchEnterResult---加密的参数为:"+param);
        String[] paramsEncode = param.split("&");
        String data = paramsEncode[0].substring(5,paramsEncode[0].length());
        String stamp = paramsEncode[1].substring(6,paramsEncode[1].length());
        //System.out.println("@MatchEnterResult---data="+data);
        //System.out.println("@MatchEnterResult---stamp="+stamp);
        HashMap<String, String> httpParams = new HashMap<String, String>();
        httpParams.put("data",data);
        httpParams.put("stamp",stamp);
        HttpHandler handler = HttpHandler.getInstance();
        //轮询result结果，两秒一次，最多轮询6次
        String enResult;
        String expect = paramAndExpect.get("expect");
        System.out.println("@MatchEnterResult---测试期望为:"+expect);
        String[] expects = expect.split(",");
        String you = expects[0];
        String me = expects[1];
        int times = 0;
        Map<String,Object> mapx ;
        do{
            Thread.sleep(2000);
            enResult = handler.usingPostMethod(ConstantURL.PANDORA_MATCH_ENTER_RESULT,httpParams,null);
            logger.info("@MatchEnterResult---请求结果:"+me+enResult);
            mapx = COMMON_GSON.fromJson(enResult, new TypeToken<HashMap<String,Object>>(){}.getType());
            //logger.info(enResult.contains("data"));
            times ++;
        }while(enResult.contains("data")==false && times<6 && mapx.get("result").toString().contains("100"));


        System.out.println("@MatchEnterResult---me:"+me);
        System.out.println("@MatchEnterResult---you:"+you);
        //匹配结果校验
        Map<String,Object> map = COMMON_GSON.fromJson(enResult, new TypeToken<HashMap<String,Object>>(){}.getType());

        //logger.info("@MatchEnterResult---result:"+map.get("result"));
        //logger.info("@MatchEnterResult---resultDesc:"+map.get("resultDesc"));
        //logger.info("@MatchEnterResult---Data:"+map.get("data"));

        String result = String.valueOf((int)Double.parseDouble(map.get("result").toString()));
        //校验code
       // Assert.assertEquals(result,paramAndExpect.get("code"));
       // Assert.assertTrue(enResult.contains(you));
    }

    @DataProvider(name = "MatchEnterData",parallel = true)
    public static Iterator<Object[]> dataMatchEnter(ITestContext context) throws IOException, BiffException {
        String xlsName = context.getCurrentXmlTest().getParameter("xlsName");
        String sheetName=context.getCurrentXmlTest().getParameter("sheetName");
        //System.out.println("@MatchEnter---"+xlsName);
        //System.out.println("@MatchEnter---"+sheetName);
        //System.out.println("@MatchEnter---测试用例路径："+PANDORA_MATCH_ENTER_DIR +xlsName);
        return GenerateParams.getMatchParams(jxlReader( PANDORA_MATCH_ENTER_DIR +xlsName,sheetName));
    }
    @DataProvider(name = "MatchEnterResultData",parallel = true)
    public static Iterator<Object[]> dataMatchEnterResult(ITestContext context) throws IOException, BiffException {
        String xlsName = context.getCurrentXmlTest().getParameter("xlsName");
        String sheetName=context.getCurrentXmlTest().getParameter("sheetName");
        //System.out.println("@MatchEnterResult---"+xlsName);
        //System.out.println("@MatchEnterResult---"+sheetName);
        //System.out.println("@MatchEnterResult---测试用例路径："+PANDORA_MATCH_ENTER_DIR +xlsName);
        return GenerateParams.getMatchResultParams(jxlReader( PANDORA_MATCH_ENTER_DIR +xlsName,sheetName));
    }

    @AfterClass
    public void AfterClassTest()
    {

        logger.info("用户游戏匹配测试结束");
    }


}
